package com.st.lotte.dao;


import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.st.lotte.model.TLO_DELY_TRCE_TRMN_IF;
import com.st.lotte.util.DBUtil;
import com.st.lotte.util.Database;
import com.st.lotte.util.LogTrace;
import com.sweettracker.commons.base.util.CommonUtils;

public class TLO_DELY_TRCE_TRMN_IF_DAO {
	private static final Logger log = LoggerFactory.getLogger(TLO_DELY_TRCE_TRMN_IF_DAO.class);
	
	Connection conn = null;
	Database db = new Database();
	PreparedStatement stmt =null;
	int maxCount = 0;
//	int sizeCount = 0;
	
	public TLO_DELY_TRCE_TRMN_IF_DAO(){
		try {
			conn = db.getConnection();
			conn.setAutoCommit(false);
			stmt= conn.prepareStatement(DBUtil.UPDATE_TRACE);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	protected synchronized void finalize() throws Throwable {
		stmt.executeBatch();
		conn.commit();
		conn.close();
	}; 
	
	public synchronized Vector<TLO_DELY_TRCE_TRMN_IF> selectALL_TLO_DELY_TRCE_TRMN_IF_DAO(String gubun) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Vector<TLO_DELY_TRCE_TRMN_IF> list = new Vector<TLO_DELY_TRCE_TRMN_IF>();
		
		try {
			String sql = DBUtil.VALIDATE_SELECTALL;
			if (gubun.equals("P")) {
				sql = DBUtil.POLL_SELECTALL;
			}
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				TLO_DELY_TRCE_TRMN_IF dtoTRCE = new TLO_DELY_TRCE_TRMN_IF();
				dtoTRCE.setCOMPANY_CODE(rs.getString("COMPANY_CODE"));
				dtoTRCE.setTRMN_DATE(rs.getDate("TRMN_DATE"));
				dtoTRCE.setCMBN_ORD_NO(rs.getString("CMBN_ORD_NO"));
				dtoTRCE.setSEQ(rs.getInt("SEQ"));
				dtoTRCE.setHODECO_CODE(rs.getString("HODECO_CODE"));
				dtoTRCE.setTRMN_BIZS_DIV_CODE(rs.getString("TRMN_BIZS_DIV_CODE"));
				dtoTRCE.setORD_NO(rs.getString("ORD_NO"));
				dtoTRCE.setORD_GOODS_SEQ(rs.getString("ORD_GOODS_SEQ"));
				dtoTRCE.setORD_GOODS_DTL_SEQ(rs.getString("ORD_GOODS_DTL_SEQ"));
				dtoTRCE.setORD_GOODS_DELY_SEQ(rs.getString("ORD_GOODS_DELY_SEQ"));
				dtoTRCE.setORD_DTL_SN(rs.getString("ORD_DTL_SN"));
				dtoTRCE.setSLIP_NO(rs.getString("SLIP_NO"));
				dtoTRCE.setSYS_BASE_CODE(rs.getString("SYS_BASE_CODE"));
				dtoTRCE.setRMK1(rs.getString("RMK1"));
				dtoTRCE.setRMK2(rs.getString("RMK2"));
				dtoTRCE.setRMK3(rs.getString("RMK3"));
				dtoTRCE.setRMK4(rs.getString("RMK4"));
				dtoTRCE.setRMK5(rs.getString("RMK5"));
				dtoTRCE.setRMK6(rs.getString("RMK6"));
				dtoTRCE.setRMK7(rs.getString("RMK7"));
				dtoTRCE.setRMK8(rs.getString("RMK8"));
				dtoTRCE.setRMK9(rs.getString("RMK9"));	
				dtoTRCE.setRMK10(rs.getString("RMK10"));
				dtoTRCE.setINTF_STAT_CODE(rs.getString("INTF_STAT_CODE"));
				dtoTRCE.setINTF_ERR_CNTT(rs.getString("INTF_ERR_CNTT"));
				dtoTRCE.setINTF_DT(rs.getDate("INTF_DT"));
				dtoTRCE.setCHGMN_ID(rs.getString("CHGMN_ID"));
				dtoTRCE.setCHG_DT(rs.getDate("CHG_DT"));
				dtoTRCE.setCNT(rs.getInt("CNT"));
				list.add(dtoTRCE);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		LogTrace.TraceLog("TRMN_OK_select");
		
		return list;
	}
	
	public synchronized void insertTLO_DELY_TRCE_TRMN_IF_DAO(TLO_DELY_TRCE_TRMN_IF dto) {
		PreparedStatement pstmt = null;
		try {
			String sql = DBUtil.INSERT_TRCE;
			pstmt = conn.prepareStatement(sql);
			int i = 1;
			pstmt.setString(i++, dto.getCOMPANY_CODE());
			pstmt.setString(i++, dto.getCMBN_ORD_NO());
			pstmt.setInt(i++, dto.getSEQ());
			pstmt.setString(i++, dto.getHODECO_CODE());
			pstmt.setString(i++, dto.getTRMN_BIZS_DIV_CODE());
			pstmt.setString(i++, dto.getORD_NO());
			pstmt.setString(i++, dto.getORD_GOODS_SEQ());
			pstmt.setString(i++, dto.getORD_GOODS_DTL_SEQ());
			pstmt.setString(i++, dto.getORD_GOODS_DELY_SEQ());
			pstmt.setString(i++, dto.getORD_DTL_SN());
			pstmt.setString(i++, dto.getSLIP_NO());
			pstmt.setString(i++, dto.getSYS_BASE_CODE());
			pstmt.setString(i++, dto.getRMK1());
			pstmt.setString(i++, dto.getRMK2());
			pstmt.setString(i++, dto.getRMK3());
			pstmt.setString(i++, dto.getRMK4());
			pstmt.setString(i++, dto.getRMK5());
			pstmt.setString(i++, dto.getRMK6());
			pstmt.setString(i++, dto.getRMK7());
			pstmt.setString(i++, dto.getRMK8());
			pstmt.setString(i++, dto.getRMK9());	
			pstmt.setString(i++, dto.getRMK10());
			pstmt.setString(i++, dto.getINTF_STAT_CODE());
			pstmt.setString(i++, dto.getINTF_ERR_CNTT());
			pstmt.setString(i++, dto.getCHGMN_ID());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			try {
				pstmt.setString(32, e.getMessage().toString());
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		LogTrace.TraceLog("TRMN_OK_insert");
	}
	
	public synchronized void updateCompleteTRMN_DAO(String COMPLETE,int CNT,String SLIP_NO, String CMB_ORD_NO) {
		Statement stmt = null;
		try {
		  String updateTableSQL = String.format("UPDATE TLO_DELY_TRCE_TRMN_IF SET CNT=%d, COMPLETE='%s' WHERE SLIP_NO='%s' AND CMBN_ORD_NO='%s' ", CNT, COMPLETE, SLIP_NO, CMB_ORD_NO);
          System.out.println(updateTableSQL);
          conn.setAutoCommit(true);

			Statement stmt2 = conn.createStatement();
			stmt2.executeUpdate(updateTableSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public synchronized void updateTLO_DELY_TRCE_TRMN_IF_DAO(String intf_code, String intf_err_cnt, String slip_no) {
		try {
			stmt.setString(1, intf_code);
			stmt.setString(2, intf_err_cnt);
			stmt.setString(3, slip_no);
			stmt.addBatch();
			
			System.out.println("maxCount===="+maxCount);
//			if (maxCount>1) {
				stmt.executeBatch();
				conn.commit();
				stmt.clearBatch();
//				maxCount = 0;
//			}else{
//				maxCount++;
//			}
		} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
		}
		LogTrace.TraceLog(" TRMN_OK_update");
	}
}
